<template>
  <div class="register-container">
    <!-- 注册内容 -->
    <div class="register">
      <h3>
        注册新用户
        <span class="go"
          >我有账号，去 <a href="login.html" target="_blank">登录</a>
        </span>
      </h3>
      <div class="content">
        <label>昵称:</label>
        <input type="text" placeholder="请输入你的昵称" v-model="nickname">
      </div>
      <div class="content">
        <label>手机号:</label>
        <input type="text" placeholder="请输入你的手机号" v-model="phoneNum" />
      </div>
      <div class="content">
        <label>验证码:</label>
        <input type="text" placeholder="请输入验证码" v-model="code" />
        <button class="vertify" @click="getCode">获取验证码</button>
      </div>
      <div class="content">
        <label>登录密码:</label>
        <input
          type="password"
          placeholder="请输入你的登录密码"
          v-model="password"
        />
      </div>
      <div class="content">
        <label>确认密码:</label>
        <input
          type="password"
          placeholder="请输入确认密码"
          v-model="confirmPassword"
        />
      </div>
      <div class="controls">
        <input name="m1" type="checkbox" :checked="agree"/>
        <span>同意协议并注册《寻亲用户协议》</span>
      </div>
      <div class="btn">
        <button @click="userRegister">完成注册</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      // 收集昵称
      nickname:'',
      // 收集手机号
      phoneNum: "",    
      // 收集验证码
      code: "",
      // 收集密码
      password: "",
      // 确认密码
      confirmPassword: "",
      // 是否同意
      agree: true,
    };
  },
  methods: {
    // 获取验证码
    async getCode() {  
      console.log('验证码test');                                                                                                                                                                                                                                                                            
      try {
        // 如果获取到验证码,至少简单判断一下
        const { phoneNum } = this;
        // dispatch里的名字是user.js里面getCode来的
        phoneNum && (await this.$store.dispatch("getCode", {phoneNum:phoneNum}));
        // axios.get('http://81.70.204.70:9050/user/sendMsg',{params:{phoneNum:phoneNum}})
        // 验证码自己填写在框中（v-model双向绑定），组件的code属性变为仓库中验证码
        // this.code = this.$store.state.user.code;
      } catch (error) {
        // 将组件中的code属性变为仓库中验证码
      }
    },
    // 用户注册的回调
    async userRegister() {
      try {
        const { nickname, phoneNum, code, password, confirmPassword } = this;
        nickname && phoneNum && code && password == confirmPassword &&(await this.$store.dispatch("userRegister", {nickname,phoneNum,code,password,confirmPassword}));
        // 如果成功---路由跳转
        this.$router.push("/login");
      } catch (error) {

      }
    },
  },
};
</script>

<style lang="less" scoped>
.register-container {
  .register {
    width: 1200px;
    height: 655px;
    border: 1px solid rgb(223, 223, 223);
    margin: 0 auto;
    padding-top: 100px;

    h3 {
      background: #ececec;
      margin: 0;
      padding: 6px 15px;
      color: #333;
      border-bottom: 1px solid #dfdfdf;
      font-size: 20.04px;
      line-height: 30.06px;

      span {
        font-size: 14px;
        float: right;

        a {
          color: #e1251b;
        }
      }
    }

    div:nth-of-type(1) {
      margin-top: 40px;
    }

    .content {
      padding-left: 390px;
      margin-bottom: 18px;
      position: relative;

      .vertify {
        width: 100px;
        height: 38px;
      }

      label {
        font-size: 14px;
        width: 96px;
        text-align: right;
        display: inline-block;
      }

      input {
        width: 270px;
        height: 38px;
        padding-left: 8px;
        box-sizing: border-box;
        margin-left: 5px;
        margin-bottom: 10px;
        outline: none;
        border: 1px solid #999;
      }

      img {
        vertical-align: sub;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .controls {
      text-align: center;
      position: relative;

      input {
        vertical-align: middle;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .btn {
      text-align: center;
      line-height: 36px;
      margin: 17px 0 0 55px;

      button {
        outline: none;
        width: 270px;
        height: 36px;
        background: #e1251b;
        color: #fff !important;
        display: inline-block;
        font-size: 16px;
      }
    }
  }

  .copyright {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    line-height: 24px;

    ul {
      li {
        display: inline-block;
        border-right: 1px solid #e4e4e4;
        padding: 0 20px;
        margin: 15px 0;
      }
    }
  }
}
</style>